/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.LiquidacionDAO;
import org.trandio.dao.LiquidacionInterface;
import org.trandio.hb.TbLiquidacion;

/**
 *
 * @author root
 */
public class ReglaServlet extends HttpServlet {

    private TbLiquidacion liquidacion;
    private LiquidacionInterface iface;
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        iface = new LiquidacionDAO();        
        liquidacion = new TbLiquidacion();
        
        
        switch(Integer.parseInt(request.getParameter("op"))){
            case 1:
                liquidacion = iface.getRegla(Integer.parseInt(request.getParameter("id")));
                Gson gson = new Gson();
                out.println(gson.toJson(liquidacion));
            break;
                
            case 2:
                
                int tipo_usuario= (request.getParameter("idtipo_usuario").equals(""))?0:Integer.parseInt(request.getParameter("idtipo_usuario"));
                int nivel= (request.getParameter("idnivel").equals(""))?0:Integer.parseInt(request.getParameter("idnivel"));                        
                int servicio= (request.getParameter("idservicio").equals(""))?0:Integer.parseInt(request.getParameter("idservicio"));
                int grupo_edad= (request.getParameter("idgrupo_edad").equals(""))?0:Integer.parseInt(request.getParameter("idgrupo_edad"));
                int tipo_afiliado= (request.getParameter("idtipo_afiliado").equals(""))?0:Integer.parseInt(request.getParameter("idtipo_afiliado"));
                int tipo_poblacion= (request.getParameter("idtipo_poblacion").equals(""))?0:Integer.parseInt(request.getParameter("idtipo_poblacion"));
                int sexo= (request.getParameter("idsexo").equals(""))?0:Integer.parseInt(request.getParameter("idsexo"));

                liquidacion.setId(Integer.parseInt(request.getParameter("id")));
                liquidacion.setIdtipoUsuario(tipo_usuario);
                liquidacion.setIdNivel(nivel);
                liquidacion.setIdServicio(servicio);
                liquidacion.setIdgrupoEdad(grupo_edad);
                liquidacion.setIdtipoAfiliacion(tipo_afiliado);
                liquidacion.setIdtipoPoblacion(tipo_poblacion);
                liquidacion.setIdSexo(sexo);
                liquidacion.setRegla(request.getParameter("descripcion"));   
                
                boolean val = iface.validarRegla(liquidacion);                
                if(val){
                    iface.setLiquidacion(liquidacion);    
                    out.println("{\"error\":\"0\"}");
                }else{
                    out.println("{\"error\":\"1\"}");
                }
                                
            break;
                
        }
        
        
        
        
        
        
        
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
